<template>
	<view class="">
		<!-- <view class="head_content">
			<CustomNavbar title="剧场" color="#fff" bg="#000"></CustomNavbar>
		</view> -->
		<web-view id='web-view' :src="baseUrl" ref="webview" v-show="isHiddn" @onPostMessage="handlePostMessage" @message="handleMessage"></web-view>
		<!-- <web-view src="" v-else></web-view> -->
    <!-- <custom-tabbar></custom-tabbar> -->
	</view>
</template>

<script>
	import store from '@/common/store/index.js'
	import { h5Address } from '@/env.js'
  import customTabbar from '../../components/custom-tabbar/custom-tabbar.vue'
	export default {
		name: '',
    components: { customTabbar, },
		data() {
			return { 
				baseUrl: '', 
				isHiddn: false ,
				webviewStyles:{ height: '650rpx' }
			}
		},
		onUnload() {
			console.log('onUnload','123')
		},
    onLoad(){
      uni.hideLoading()
      setTimeout(() => {
        uni.showLoading({ title:'Loading...' })
      }, 100);
      // uni.hideTabBar()
    },
		onShow() {
			console.log(getCurrentPages(),'getCurrentPages')
			// uni.setTabBarStyle({
			// 	color: '#999',
			// 	selectedColor: '#fff',
			// 	backgroundColor: '#000',
			// 	borderStyle: '#000'
			// })
			const token = store.state.user.token || ''
			const {
				videoAutoplay,
				config
			} = store.state.app
			// if (!token) {
			// 	store.dispatch('user/logout')
			// 	uni.showModal({
			// 		title: '系统提示',
			// 		content: '本操作需要您进行登录验证',
			// 		success: res => {
			// 			if (res.confirm) {
			// 				uni.navigateTo({ url: '/pages/login/login' })
			// 			}
			// 		}
			// 	})
			// 	return
			// }
			this.isHiddn = true
			this.baseUrl = `${h5Address}?token=${token}&config=${config}&videoAutoplay=${videoAutoplay}`
			// this.baseUrl =
			// 	`https://njhengyu.top/app/#/pages/video/recommend?token=${token}&config=${config}&videoAutoplay=${videoAutoplay}`
		},
    onReady() {
			//获取webview页面
			var currentWebview = this.$mp.page.$getAppWebview().children()[0]
			 
			console.log(JSON.stringify(currentWebview),'currentWebview')
			//监听加载完毕  关闭loading
			currentWebview.addEventListener('loaded', function() {
				uni.hideLoading()
				// plus.nativeUI.closeWaiting()
			}, false)
		},
		onHide() {
			let vm = this
			this.isHiddn = false
			const token = store.state.user.token || ''
			const {
				videoAutoplay,
				config
			} = store.state.app
			vm.baseUrl = `${h5Address}?token=${token}&config=${config}&videoAutoplay=${videoAutoplay}&close=${true}`
			setTimeout(function(){
				vm.isHiddn = true
			},1000)
			// this.baseUrl = `${h5Address}?token=${token}&config=${config}&videoAutoplay=${videoAutoplay}&close=${true}`
			// uni.destroyWebView({ id: 'web-view'			})
			// this.isHiddn = false
		  // this.tabChange = false 
		  // uni.setTabBarStyle({
		  // 	color: '#999',
		  // 	selectedColor: '#EE7F33',
		  // 	backgroundColor: '#000',
		  // 	borderStyle: '#000',
		  // })
		},
		methods: {
			// webview向外部发送消
			handlePostMessage: function(data) {
				console.log('接收到消息：' + JSON.stringify(data.detail))
			},
			handleMessage(data) {
				console.log('接收到消息：' + JSON.stringify(data.detail))
				if (data.detail.data) {
					const detail = data.detail.data[0]
					detail.navType && uni[detail.navType]({ url: detail.url })
					
					if(detail.type == 'back'){
						uni.navigateBack()
					}else if(detail.type == 'home'){
						uni.switchTab({ url: '/pages/home/index' })
					}
				}
				console.log('handleMessage33', data.detail.data[0].type)
			}
		},
		components: {},
	}
</script>

<style>
page{
	min-height: 100vh;
	background-color: #000;
}
</style>